package org.framework.server.util;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.framework.core.util.DateUtil;
import org.framework.define.Constant;
import org.framework.define.DateFormat;
import org.framework.define.bean.entity.TblTaskLowest;
import org.framework.define.bean.entity.TblTaskRule;
import org.framework.define.bean.medium.JsonProduct;
import org.framework.server.memory.Memory;
import org.framework.server.util.http.HttpRequest;

public class TaskUtil {
	
	public final static String TableName;
	
	static {
		TableName = "record.tbl_task_" + DateUtil.getDate(DateFormat.YMD.toString());
	}
	
	public static String generateTaskId(){
		return "T"+Memory.SERVER_ID + DateUtil.getDate(DateFormat.YMDHMSsss) + String.valueOf(Math.random()).substring(2,6);
	}
	
	//计算价格计算最低消耗
	public static BigDecimal countLowestCoins(BigDecimal price){
		for (TblTaskLowest taskLowest : Memory.LOWEST_LIST) {
			if(price.compareTo(taskLowest.getPrice()) < 1){
				return taskLowest.getCoins();
			}
		}
		return new BigDecimal(1);
	}
	
	//计算规则消耗金币
	public static void countRuleCoins(String ruleName, BigDecimal payCoins, BigDecimal spendCoins, BigDecimal lowest){
		TblTaskRule rule = Memory.RULE_MAP.get(ruleName);
		if(rule != null){
			if(rule.getRuleType() == Constant.Task.Rule.Type.PAY){
				payCoins = payCoins.add(lowest.multiply(rule.getRuleMultCoins())).add(rule.getRuleCoins());
			}else{
				spendCoins = spendCoins.add(lowest.multiply(rule.getRuleMultCoins())).add(rule.getRuleCoins());
			}
		}
	}
	
	public static JsonProduct shopCrawler(String url) throws Exception{
		JsonProduct product = new JsonProduct();
		
		String [] urlSplit = url.split("[?]");
		if(urlSplit == null || urlSplit.length < 2)
			return product;
		String[] params = urlSplit[1].split("[&]");
		for (String param : params) {
			if(param.contains("id"))
				product.setShopId(param.split("[=]")[1]);
		}
		
		String result = new HttpRequest().httpGZIPGet(url, null, "gbk");
		
		Pattern p = Pattern.compile("sellerNick:\"(.+)\",|price:(.+),|<img id=\"J_ImgBooth\" data-src=\"(.+)\"  |<title>(.+)</title>", Pattern.MULTILINE);
	    Matcher m = p.matcher(result);
	    while(m.find()) {
	    	String first = m.group(0);
	    	
	    	String find = null;
	    	for (int i = 1; i <= m.groupCount(); i++) {
	    		find = m.group(i);
	    		if(find != null)
	    			break;
			}
	    	
	    	if(first.contains("sellerNick"))
	    		product.setSeller(find);
	    	else if(first.contains("title"))
	    		product.setName(find);
	    	else if(first.contains("price"))
	    		product.setPrice(new BigDecimal(find));
	    	else if(first.contains("img"))
	    		product.setImgUrl(find);
	    }
	    return product;
	}
	
	public static void main(String[] args) {
		String url = "http://item.taobao.com/item.htm?spm=a230r.1.14.96.q4RhbH&id39285517138&ns=1#detail";
		String [] urlSplit = url.split("[?]");
		if(urlSplit == null || urlSplit.length < 2)
			System.out.println();
		String[] params = urlSplit[1].split("[&]");
		for (String param : params) {
			if(param.contains("id"))
				System.out.println(param.split("[=]")[1]);
		}
	}
}
